{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4e4c12bb",
   "metadata": {},
   "source": [
    "* 记录人：LZ\n",
    "* 时间：2022-3-15\n",
    "* week02: 人脸分析&人脸比对\n",
    "* 学习资源：https://console.faceplusplus.com.cn/documents/4888383（人脸分析）  \n",
    "https://console.faceplusplus.com.cn/documents/4887586（人脸比对）  \n",
    "https://console.faceplusplus.com.cn/documents/4888381(人脸搜索)  \n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6ba34646",
   "metadata": {},
   "source": [
    "# 上周回顾\n",
    "### Face++ 实践  \n",
    "#### 人脸检测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "b395f7de",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647311502,417b4030-21c3-4878-86eb-3cf6bfbdcb26',\n",
       " 'time_used': 285,\n",
       " 'faces': [{'face_token': '2dca804c644f461e3c78e3d31e7cb98c',\n",
       "   'face_rectangle': {'top': 131, 'left': 96, 'width': 144, 'height': 144},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 57},\n",
       "    'smile': {'value': 0.043, 'threshold': 50.0}}}],\n",
       " 'image_id': '7s2QPXIwulPvCvAOhZf+gg==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1.导入需要的requests 模块\n",
    "import requests\n",
    "\n",
    "# 2.输入我们需要API网站注册的API_Key\n",
    "API_key = \"biwZehUGhGp1ZwtyL0OaDDtSsod-tqIz\"\n",
    "API_secret = \"-KEyjyw1V66uzlHIAMuE95MfjePAk71B\"\n",
    "\n",
    "# 3.准备API测试接口的urlr\n",
    "face_detect = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "\n",
    "# 4.调用方法 POST\n",
    "\n",
    "# 5.准备url传递参数\n",
    "payload = {\n",
    "    \"api_key\":\"biwZehUGhGp1ZwtyL0OaDDtSsod-tqIz\",\n",
    "    \"api_secret\":\"-KEyjyw1V66uzlHIAMuE95MfjePAk71B\",\n",
    "    \"image_url\":\"http://images.ofweek.com/Upload/News/2018-02/13/Sunny/1518506677715044339.jpg\",\n",
    "    \"return_attributes\":\"gender,age,smiling\"\n",
    "}\n",
    "\n",
    "# 6.发起请求\n",
    "r = requests.post(url=face_detect,params=payload)\n",
    "r\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b7554f26",
   "metadata": {},
   "source": [
    "# 本周知识点  \n",
    "* 人脸分析API    \n",
    "* 人脸对比API    \n",
    "\n",
    "\n",
    "##### 返回值说明 \n",
    " * face_tokens ：人脸标识，由一个或多个人脸标识组成，用逗号分隔；最多支持 5 个 face_token(即一张图片里最多标识五个人脸）；具有时效性每张图片的face_token只保存72小时，之后会失效，需要将它存起来。\n",
    " * request_id ：用于区分每一次请求的唯一的字符串。每检测分析一次就会更新一次  \n",
    " * time_used ： 整个请求所花费的时间，单位为毫秒。  \n",
    " * faces ： 经过分析的人脸数组   注：如果没有检测出人脸则为空数组  \n",
    " * error_message ：请求失败"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8fa1bd2e",
   "metadata": {},
   "source": [
    "### 1.人脸分析API  \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "128fa682",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 232,\n",
       " 'request_id': '1647314422,3db1d8a3-1d97-4c89-a8d0-afdc3db6c7a5',\n",
       " 'faces': [{'attributes': {'emotion': {'sadness': 19.885,\n",
       "     'neutral': 24.068,\n",
       "     'disgust': 0.029,\n",
       "     'anger': 0.029,\n",
       "     'surprise': 0.029,\n",
       "     'fear': 55.92,\n",
       "     'happiness': 0.04},\n",
       "    'beauty': {'female_score': 26.22, 'male_score': 24.384},\n",
       "    'gender': {'value': 'Female'},\n",
       "    'age': {'value': 57},\n",
       "    'mouthstatus': {'close': 96.397,\n",
       "     'surgical_mask_or_respirator': 0.0,\n",
       "     'open': 3.573,\n",
       "     'other_occlusion': 0.03},\n",
       "    'glass': {'value': 'None'},\n",
       "    'skinstatus': {'dark_circle': 4.479,\n",
       "     'stain': 98.795,\n",
       "     'acne': 99.817,\n",
       "     'health': 0.183},\n",
       "    'headpose': {'yaw_angle': 2.7335029,\n",
       "     'pitch_angle': 0.7519013,\n",
       "     'roll_angle': -4.8993597},\n",
       "    'blur': {'blurness': {'threshold': 50.0, 'value': 0.076},\n",
       "     'motionblur': {'threshold': 50.0, 'value': 0.076},\n",
       "     'gaussianblur': {'threshold': 50.0, 'value': 0.076}},\n",
       "    'smile': {'threshold': 50.0, 'value': 0.006},\n",
       "    'eyestatus': {'left_eye_status': {'normal_glass_eye_open': 0.0,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.0,\n",
       "      'no_glass_eye_open': 100.0,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0},\n",
       "     'right_eye_status': {'normal_glass_eye_open': 0.0,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.0,\n",
       "      'no_glass_eye_open': 100.0,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0}},\n",
       "    'facequality': {'threshold': 70.1, 'value': 93.429},\n",
       "    'eyegaze': {'right_eye_gaze': {'position_x_coordinate': 0.503,\n",
       "      'vector_z_component': 0.967,\n",
       "      'vector_x_component': 0.065,\n",
       "      'vector_y_component': -0.248,\n",
       "      'position_y_coordinate': 0.358},\n",
       "     'left_eye_gaze': {'position_x_coordinate': 0.481,\n",
       "      'vector_z_component': 0.982,\n",
       "      'vector_x_component': -0.103,\n",
       "      'vector_y_component': -0.156,\n",
       "      'position_y_coordinate': 0.41}}},\n",
       "   'face_rectangle': {'width': 144, 'top': 131, 'left': 96, 'height': 144},\n",
       "   'face_token': '2dca804c644f461e3c78e3d31e7cb98c'}]}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1.准备url\n",
    "base_url = \"https://api-cn.faceplusplus.com/facepp/v3/face/analyze\"\n",
    "\n",
    "# 2.API账户/通行证\n",
    "API_key = \"biwZehUGhGp1ZwtyL0OaDDtSsod-tqIz\"\n",
    "API_secret = \"-KEyjyw1V66uzlHIAMuE95MfjePAk71B\"\n",
    "\n",
    "# 3.调用方法POST\n",
    "\n",
    "# 4.所有 API Key 都可以调用本 API。\n",
    "#   其中 calculate_all 和 face_rectangle 参数只有正式 API Key 才能使用，试用 API Key 无法使用。\n",
    "\n",
    "# 5.payload传递参数\n",
    "payload = {\n",
    "    \"api_key\":\"biwZehUGhGp1ZwtyL0OaDDtSsod-tqIz\",\n",
    "    \"api_secret\":\"-KEyjyw1V66uzlHIAMuE95MfjePAk71B\",\n",
    "    \"face_tokens\":\"2dca804c644f461e3c78e3d31e7cb98c\",\n",
    "    \"return_attributes\":\"gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus\",\n",
    "    \"beauty_score_min\":0,\n",
    "    \"beauty_score_max\":100\n",
    "}\n",
    "r_analyze = requests.post(url=base_url,params=payload)\n",
    "r_analyze\n",
    "r_analyze.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d77ca11d",
   "metadata": {},
   "source": [
    "### 2.人脸对比API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "2cc1cc8b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647315130,e4cc4091-ad5d-4b43-b0f6-cfad9269d5fc',\n",
       " 'time_used': 346,\n",
       " 'faces': [{'face_token': '510e0f1ab1a50394c215b9df4a0bbe42',\n",
       "   'face_rectangle': {'top': 92, 'left': 54, 'width': 80, 'height': 80},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 42},\n",
       "    'smile': {'value': 100.0, 'threshold': 50.0}}}],\n",
       " 'image_id': 'xl5/E8Abwz7//Rol73SIKQ==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 第二张人脸图片的人脸检测（用作人脸对比）\n",
    "\n",
    "# 1.导入需要的requests 模块\n",
    "import requests\n",
    "\n",
    "# 2.输入我们需要API网站注册的API_Key\n",
    "API_key = \"biwZehUGhGp1ZwtyL0OaDDtSsod-tqIz\"\n",
    "API_secret = \"-KEyjyw1V66uzlHIAMuE95MfjePAk71B\"\n",
    "\n",
    "# 3.准备API测试接口的urlr\n",
    "face_detect = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "\n",
    "# 4.调用方法 POST\n",
    "\n",
    "# 5.准备url传递参数\n",
    "payload = {\n",
    "    \"api_key\":\"biwZehUGhGp1ZwtyL0OaDDtSsod-tqIz\",\n",
    "    \"api_secret\":\"-KEyjyw1V66uzlHIAMuE95MfjePAk71B\",\n",
    "    \"image_url\":\"http://img.mp.itc.cn/upload/20170116/4cac5f66b3d843e7b13061c3cba58ef5.jpg\",\n",
    "    \"return_attributes\":\"gender,age,smiling\"\n",
    "}\n",
    "\n",
    "# 6.发起请求\n",
    "r = requests.post(url=face_detect,params=payload)\n",
    "r\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "25147413",
   "metadata": {},
   "outputs": [],
   "source": [
    "# "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "4231558b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647315167,602f2b0d-ff05-435b-91b0-7a2c1c9251e2',\n",
       " 'time_used': 465,\n",
       " 'confidence': 52.864,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1.准备url\n",
    "base_url = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "\n",
    "# 2.API账户/通行证\n",
    "API_key = \"biwZehUGhGp1ZwtyL0OaDDtSsod-tqIz\"\n",
    "API_secret = \"-KEyjyw1V66uzlHIAMuE95MfjePAk71B\"\n",
    "\n",
    "# 3.调用方法POST\n",
    "\n",
    "# 4.所有 API Key 都可以调用本 API。\n",
    "#   其中 calculate_all 和 face_rectangle 参数只有正式 API Key 才能使用，试用 API Key 无法使用。\n",
    "\n",
    "# 5.payload传递参数\n",
    "payload = {\n",
    "    \"api_key\":\"biwZehUGhGp1ZwtyL0OaDDtSsod-tqIz\",\n",
    "    \"api_secret\":\"-KEyjyw1V66uzlHIAMuE95MfjePAk71B\",\n",
    "    \"face_token1\":\"2dca804c644f461e3c78e3d31e7cb98c\",   # 第一张图片人脸的face_token1\n",
    "    \"face_token2\":\"510e0f1ab1a50394c215b9df4a0bbe42\"    # 第二张图片人脸的face_tokrn2\n",
    "}\n",
    "r_analyze = requests.post(url=base_url,params=payload)\n",
    "r_analyze\n",
    "r_analyze.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18657073",
   "metadata": {},
   "source": [
    "### 3. 封装应用函数（内部使用的接口）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f34c93a",
   "metadata": {},
   "source": [
    "#### 1.人脸检测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "41854019",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 准备工作\n",
    "API_key = \"biwZehUGhGp1ZwtyL0OaDDtSsod-tqIz\"\n",
    "API_sercret = \"-KEyjyw1V66uzlHIAMuE95MfjePAk71B\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "12010df7",
   "metadata": {},
   "outputs": [],
   "source": [
    "def face_detect(API_key,API_sercret,image_url,landmark=None,attributes=None):\n",
    "    \"\"\"face++ 人脸检测\"\"\"\n",
    "    import requests\n",
    "    url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    payload = {\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_sercret,\n",
    "        'image_url':image_url,\n",
    "        'return_landmark':landmark,\n",
    "        'return_attributes':attributes\n",
    "    }\n",
    "    r = requests.post(url,params=payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "8829c1f5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647342492,5f817afe-193b-4e9d-a3a0-6d031526ca38',\n",
       " 'time_used': 136,\n",
       " 'faces': [{'face_token': '6c3f120c72a6625ec17db94c228f5a03',\n",
       "   'face_rectangle': {'top': 150, 'left': 208, 'width': 201, 'height': 201}}],\n",
       " 'image_id': 'EK6u+pcbv+4ZJQwtG5iaaw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_detect(API_key,API_sercret,'http://5b0988e595225.cdn.sohucs.com/q_70,c_zoom,w_640/images/20180620/a7fcd23bbc7c4bf9b5818d6870c4763e.jpeg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "9aa92a8b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647342476,f0bb9301-4bd9-4f05-a1b6-b85acb87dd5d',\n",
       " 'time_used': 1389,\n",
       " 'faces': [{'face_token': 'e55550ac3cd3a8b0e6e05e6185f155c6',\n",
       "   'face_rectangle': {'top': 274, 'left': 149, 'width': 407, 'height': 407}}],\n",
       " 'image_id': 'ebOrBiHOGj8ZNNGXZEjltA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_detect(API_key,API_sercret,'http://attachbak.dataguru.cn/attachments/portal/201812/29/161729gqqfq4qa4oli51oi.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "cec2cae3",
   "metadata": {},
   "outputs": [],
   "source": [
    "face01 = face_detect(API_key,API_sercret,'http://5b0988e595225.cdn.sohucs.com/q_70,c_zoom,w_640/images/20180620/a7fcd23bbc7c4bf9b5818d6870c4763e.jpeg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "7f42fc63",
   "metadata": {},
   "outputs": [],
   "source": [
    "face02 = face_detect(API_key,API_sercret,'http://attachbak.dataguru.cn/attachments/portal/201812/29/161729gqqfq4qa4oli51oi.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "8a133d49",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'08d078ea6d9fb756e9b15ffcd627bf31'"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 可以使用字典键值对查询\n",
    "face01['faces'][0]['face_token']    # face_token是会变化的，每检测一次就会变化一次"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "1504a9dc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'e7af458b3ab686e7f88e1f94f89c740c'"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face02['faces'][0]['face_token']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "213f9edf",
   "metadata": {},
   "source": [
    "#### 2.人脸分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "d346eccc",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_key = \"biwZehUGhGp1ZwtyL0OaDDtSsod-tqIz\"\n",
    "API_sercret = \"-KEyjyw1V66uzlHIAMuE95MfjePAk71B\"\n",
    "\n",
    "def face_detect(API_key,API_sercret,face_token,landmark=1,attributes='gender,age,emotion'):\n",
    "    \"\"\"face++ 人脸分析\"\"\"\n",
    "    import requests\n",
    "    url = 'https://api-cn.faceplusplus.com/facepp/v3/face/analyze'\n",
    "    payload = {\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_sercret,\n",
    "        'face_tokens':face_token,\n",
    "        'return_landmark':landmark,\n",
    "        'return_attributes':attributes\n",
    "    }\n",
    "    r = requests.post(url,params=payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "deede9d6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 254,\n",
       " 'request_id': '1647344709,75fa28ee-72f6-4423-9e76-4fe809a3b5fe',\n",
       " 'faces': [{'landmark': {'mouth_upper_lip_left_contour2': {'y': 520, 'x': 297},\n",
       "    'mouth_upper_lip_left_contour3': {'y': 528, 'x': 308},\n",
       "    'mouth_lower_lip_right_contour3': {'y': 575, 'x': 395},\n",
       "    'mouth_upper_lip_left_contour1': {'y': 516, 'x': 335},\n",
       "    'left_eye_upper_left_quarter': {'y': 334, 'x': 240},\n",
       "    'left_eyebrow_lower_middle': {'y': 300, 'x': 246},\n",
       "    'contour_chin': {'y': 681, 'x': 354},\n",
       "    'left_eyebrow_lower_left_quarter': {'y': 304, 'x': 214},\n",
       "    'right_eyebrow_lower_left_quarter': {'y': 302, 'x': 430},\n",
       "    'mouth_lower_lip_right_contour1': {'y': 550, 'x': 402},\n",
       "    'mouth_lower_lip_left_contour2': {'y': 556, 'x': 284},\n",
       "    'left_eye_bottom': {'y': 353, 'x': 260},\n",
       "    'mouth_lower_lip_bottom': {'y': 583, 'x': 355},\n",
       "    'contour_left9': {'y': 672, 'x': 302},\n",
       "    'mouth_lower_lip_top': {'y': 560, 'x': 355},\n",
       "    'right_eyebrow_upper_middle': {'y': 280, 'x': 463},\n",
       "    'right_eyebrow_left_corner': {'y': 299, 'x': 398},\n",
       "    'right_eye_bottom': {'y': 353, 'x': 446},\n",
       "    'contour_left7': {'y': 615, 'x': 222},\n",
       "    'contour_left6': {'y': 578, 'x': 192},\n",
       "    'contour_left5': {'y': 535, 'x': 171},\n",
       "    'contour_left4': {'y': 488, 'x': 159},\n",
       "    'contour_left3': {'y': 440, 'x': 153},\n",
       "    'contour_left2': {'y': 392, 'x': 151},\n",
       "    'contour_left1': {'y': 345, 'x': 152},\n",
       "    'left_eye_lower_left_quarter': {'y': 350, 'x': 239},\n",
       "    'mouth_upper_lip_top': {'y': 519, 'x': 355},\n",
       "    'contour_right3': {'y': 443, 'x': 550},\n",
       "    'contour_right2': {'y': 396, 'x': 553},\n",
       "    'mouth_left_corner': {'y': 528, 'x': 262},\n",
       "    'contour_right4': {'y': 489, 'x': 543},\n",
       "    'contour_right7': {'y': 613, 'x': 481},\n",
       "    'left_eyebrow_left_corner': {'y': 310, 'x': 184},\n",
       "    'nose_right': {'y': 456, 'x': 412},\n",
       "    'right_eye_upper_right_quarter': {'y': 334, 'x': 466},\n",
       "    'nose_tip': {'y': 452, 'x': 354},\n",
       "    'contour_right5': {'y': 534, 'x': 530},\n",
       "    'nose_contour_lower_middle': {'y': 483, 'x': 355},\n",
       "    'right_eye_top': {'y': 330, 'x': 445},\n",
       "    'mouth_lower_lip_left_contour3': {'y': 576, 'x': 314},\n",
       "    'right_eye_right_corner': {'y': 345, 'x': 484},\n",
       "    'right_eye_lower_right_quarter': {'y': 350, 'x': 466},\n",
       "    'mouth_upper_lip_right_contour2': {'y': 518, 'x': 410},\n",
       "    'right_eyebrow_lower_right_quarter': {'y': 305, 'x': 492},\n",
       "    'left_eye_left_corner': {'y': 344, 'x': 221},\n",
       "    'mouth_right_corner': {'y': 525, 'x': 443},\n",
       "    'mouth_upper_lip_right_contour3': {'y': 527, 'x': 399},\n",
       "    'right_eye_lower_left_quarter': {'y': 351, 'x': 426},\n",
       "    'left_eyebrow_right_corner': {'y': 300, 'x': 312},\n",
       "    'left_eyebrow_lower_right_quarter': {'y': 301, 'x': 279},\n",
       "    'right_eye_center': {'y': 344, 'x': 446},\n",
       "    'left_eye_upper_right_quarter': {'y': 335, 'x': 283},\n",
       "    'mouth_lower_lip_left_contour1': {'y': 551, 'x': 305},\n",
       "    'contour_left8': {'y': 647, 'x': 259},\n",
       "    'nose_left': {'y': 458, 'x': 299},\n",
       "    'right_eyebrow_lower_middle': {'y': 301, 'x': 462},\n",
       "    'left_eye_top': {'y': 330, 'x': 262},\n",
       "    'left_eye_center': {'y': 344, 'x': 261},\n",
       "    'left_eye_lower_right_quarter': {'y': 352, 'x': 281},\n",
       "    'nose_contour_right1': {'y': 350, 'x': 383},\n",
       "    'contour_right9': {'y': 672, 'x': 406},\n",
       "    'right_eye_left_corner': {'y': 347, 'x': 409},\n",
       "    'left_eyebrow_upper_left_quarter': {'y': 285, 'x': 210},\n",
       "    'left_eye_pupil': {'y': 340, 'x': 262},\n",
       "    'right_eyebrow_upper_left_quarter': {'y': 282, 'x': 429},\n",
       "    'contour_right8': {'y': 646, 'x': 447},\n",
       "    'right_eyebrow_right_corner': {'y': 313, 'x': 520},\n",
       "    'right_eye_upper_left_quarter': {'y': 334, 'x': 425},\n",
       "    'left_eyebrow_upper_middle': {'y': 278, 'x': 245},\n",
       "    'right_eyebrow_upper_right_quarter': {'y': 288, 'x': 496},\n",
       "    'nose_contour_left1': {'y': 351, 'x': 324},\n",
       "    'nose_contour_left2': {'y': 427, 'x': 311},\n",
       "    'mouth_upper_lip_right_contour1': {'y': 516, 'x': 375},\n",
       "    'contour_right1': {'y': 350, 'x': 553},\n",
       "    'nose_contour_right2': {'y': 425, 'x': 398},\n",
       "    'mouth_lower_lip_right_contour2': {'y': 554, 'x': 422},\n",
       "    'contour_right6': {'y': 576, 'x': 508},\n",
       "    'nose_contour_right3': {'y': 472, 'x': 386},\n",
       "    'nose_contour_left3': {'y': 473, 'x': 325},\n",
       "    'left_eye_right_corner': {'y': 348, 'x': 299},\n",
       "    'left_eyebrow_upper_right_quarter': {'y': 281, 'x': 281},\n",
       "    'right_eye_pupil': {'y': 340, 'x': 444},\n",
       "    'mouth_upper_lip_bottom': {'y': 532, 'x': 355}},\n",
       "   'attributes': {'emotion': {'sadness': 0.0,\n",
       "     'neutral': 0.0,\n",
       "     'disgust': 0.0,\n",
       "     'anger': 0.0,\n",
       "     'surprise': 0.0,\n",
       "     'fear': 0.0,\n",
       "     'happiness': 100.0},\n",
       "    'gender': {'value': 'Female'},\n",
       "    'age': {'value': 30}},\n",
       "   'face_rectangle': {'width': 407, 'top': 274, 'left': 149, 'height': 407},\n",
       "   'face_token': 'e7af458b3ab686e7f88e1f94f89c740c'}]}"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_detect(API_key,API_sercret,'e7af458b3ab686e7f88e1f94f89c740c')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "80a61010",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 225,\n",
       " 'request_id': '1647344791,4aeed25b-1121-47ef-ac05-175269a394d4',\n",
       " 'faces': [{'landmark': {'mouth_upper_lip_left_contour2': {'y': 282, 'x': 275},\n",
       "    'mouth_upper_lip_left_contour3': {'y': 289, 'x': 280},\n",
       "    'mouth_lower_lip_right_contour3': {'y': 302, 'x': 314},\n",
       "    'mouth_upper_lip_left_contour1': {'y': 280, 'x': 288},\n",
       "    'left_eye_upper_left_quarter': {'y': 184, 'x': 244},\n",
       "    'left_eyebrow_lower_middle': {'y': 164, 'x': 249},\n",
       "    'contour_chin': {'y': 348, 'x': 299},\n",
       "    'left_eyebrow_lower_left_quarter': {'y': 164, 'x': 235},\n",
       "    'right_eyebrow_lower_left_quarter': {'y': 167, 'x': 336},\n",
       "    'mouth_lower_lip_right_contour1': {'y': 289, 'x': 317},\n",
       "    'mouth_lower_lip_left_contour2': {'y': 295, 'x': 271},\n",
       "    'left_eye_bottom': {'y': 196, 'x': 253},\n",
       "    'mouth_lower_lip_bottom': {'y': 306, 'x': 298},\n",
       "    'contour_left9': {'y': 341, 'x': 276},\n",
       "    'mouth_lower_lip_top': {'y': 291, 'x': 298},\n",
       "    'right_eyebrow_upper_middle': {'y': 153, 'x': 354},\n",
       "    'right_eyebrow_left_corner': {'y': 167, 'x': 318},\n",
       "    'right_eye_bottom': {'y': 196, 'x': 348},\n",
       "    'contour_left7': {'y': 309, 'x': 247},\n",
       "    'contour_left6': {'y': 291, 'x': 235},\n",
       "    'contour_left5': {'y': 272, 'x': 227},\n",
       "    'contour_left4': {'y': 251, 'x': 221},\n",
       "    'contour_left3': {'y': 230, 'x': 218},\n",
       "    'contour_left2': {'y': 210, 'x': 215},\n",
       "    'contour_left1': {'y': 188, 'x': 215},\n",
       "    'left_eye_lower_left_quarter': {'y': 193, 'x': 243},\n",
       "    'mouth_upper_lip_top': {'y': 282, 'x': 297},\n",
       "    'contour_right3': {'y': 238, 'x': 400},\n",
       "    'contour_right2': {'y': 217, 'x': 404},\n",
       "    'mouth_left_corner': {'y': 285, 'x': 261},\n",
       "    'contour_right4': {'y': 260, 'x': 394},\n",
       "    'contour_right7': {'y': 317, 'x': 360},\n",
       "    'left_eyebrow_left_corner': {'y': 166, 'x': 221},\n",
       "    'nose_right': {'y': 254, 'x': 323},\n",
       "    'right_eye_upper_right_quarter': {'y': 184, 'x': 359},\n",
       "    'nose_tip': {'y': 248, 'x': 296},\n",
       "    'contour_right5': {'y': 281, 'x': 386},\n",
       "    'nose_contour_lower_middle': {'y': 265, 'x': 297},\n",
       "    'right_eye_top': {'y': 182, 'x': 347},\n",
       "    'mouth_lower_lip_left_contour3': {'y': 303, 'x': 282},\n",
       "    'right_eye_right_corner': {'y': 190, 'x': 369},\n",
       "    'right_eye_lower_right_quarter': {'y': 194, 'x': 359},\n",
       "    'mouth_upper_lip_right_contour2': {'y': 282, 'x': 322},\n",
       "    'right_eyebrow_lower_right_quarter': {'y': 166, 'x': 371},\n",
       "    'left_eye_left_corner': {'y': 189, 'x': 235},\n",
       "    'mouth_right_corner': {'y': 284, 'x': 338},\n",
       "    'mouth_upper_lip_right_contour3': {'y': 288, 'x': 317},\n",
       "    'right_eye_lower_left_quarter': {'y': 195, 'x': 337},\n",
       "    'left_eyebrow_right_corner': {'y': 167, 'x': 280},\n",
       "    'left_eyebrow_lower_right_quarter': {'y': 166, 'x': 264},\n",
       "    'right_eye_center': {'y': 190, 'x': 348},\n",
       "    'left_eye_upper_right_quarter': {'y': 186, 'x': 265},\n",
       "    'mouth_lower_lip_left_contour1': {'y': 289, 'x': 280},\n",
       "    'contour_left8': {'y': 327, 'x': 260},\n",
       "    'nose_left': {'y': 254, 'x': 274},\n",
       "    'right_eyebrow_lower_middle': {'y': 165, 'x': 354},\n",
       "    'left_eye_top': {'y': 182, 'x': 255},\n",
       "    'left_eye_center': {'y': 190, 'x': 254},\n",
       "    'left_eye_lower_right_quarter': {'y': 196, 'x': 263},\n",
       "    'nose_contour_right1': {'y': 195, 'x': 312},\n",
       "    'contour_right9': {'y': 345, 'x': 324},\n",
       "    'right_eye_left_corner': {'y': 194, 'x': 327},\n",
       "    'left_eyebrow_upper_left_quarter': {'y': 155, 'x': 234},\n",
       "    'left_eye_pupil': {'y': 189, 'x': 255},\n",
       "    'right_eyebrow_upper_left_quarter': {'y': 156, 'x': 334},\n",
       "    'contour_right8': {'y': 332, 'x': 343},\n",
       "    'right_eyebrow_right_corner': {'y': 169, 'x': 387},\n",
       "    'right_eye_upper_left_quarter': {'y': 185, 'x': 335},\n",
       "    'left_eyebrow_upper_middle': {'y': 153, 'x': 250},\n",
       "    'right_eyebrow_upper_right_quarter': {'y': 156, 'x': 373},\n",
       "    'nose_contour_left1': {'y': 195, 'x': 286},\n",
       "    'nose_contour_left2': {'y': 238, 'x': 279},\n",
       "    'mouth_upper_lip_right_contour1': {'y': 279, 'x': 307},\n",
       "    'contour_right1': {'y': 194, 'x': 406},\n",
       "    'nose_contour_right2': {'y': 238, 'x': 318},\n",
       "    'mouth_lower_lip_right_contour2': {'y': 294, 'x': 327},\n",
       "    'contour_right6': {'y': 300, 'x': 374},\n",
       "    'nose_contour_right3': {'y': 261, 'x': 311},\n",
       "    'nose_contour_left3': {'y': 260, 'x': 285},\n",
       "    'left_eye_right_corner': {'y': 195, 'x': 272},\n",
       "    'left_eyebrow_upper_right_quarter': {'y': 157, 'x': 267},\n",
       "    'right_eye_pupil': {'y': 188, 'x': 347},\n",
       "    'mouth_upper_lip_bottom': {'y': 291, 'x': 298}},\n",
       "   'attributes': {'emotion': {'sadness': 0.006,\n",
       "     'neutral': 67.522,\n",
       "     'disgust': 0.003,\n",
       "     'anger': 0.003,\n",
       "     'surprise': 0.044,\n",
       "     'fear': 0.003,\n",
       "     'happiness': 32.42},\n",
       "    'gender': {'value': 'Female'},\n",
       "    'age': {'value': 21}},\n",
       "   'face_rectangle': {'width': 201, 'top': 150, 'left': 208, 'height': 201},\n",
       "   'face_token': '08d078ea6d9fb756e9b15ffcd627bf31'}]}"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_detect(API_key,API_sercret,'08d078ea6d9fb756e9b15ffcd627bf31')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "46d601fe",
   "metadata": {},
   "source": [
    "#### 3.人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "79468106",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_key = \"biwZehUGhGp1ZwtyL0OaDDtSsod-tqIz\"\n",
    "API_sercret = \"-KEyjyw1V66uzlHIAMuE95MfjePAk71B\"\n",
    "\n",
    "def face_detect(API_key,API_sercret,face_token1,face_token2):\n",
    "    \"\"\"face++ 人脸对比\"\"\"\n",
    "    import requests\n",
    "    url = 'https://api-cn.faceplusplus.com/facepp/v3/compare'\n",
    "    payload = {\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_sercret,\n",
    "        'face_token1':face_token1,\n",
    "        'face_token2':face_token2\n",
    "    }\n",
    "    r = requests.post(url,params=payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "07242001",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647345710,359c3282-8c52-4c59-90f3-27f2df35513a',\n",
       " 'time_used': 317,\n",
       " 'confidence': 37.315,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_detect(API_key,API_sercret,'08d078ea6d9fb756e9b15ffcd627bf31','e7af458b3ab686e7f88e1f94f89c740c')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "3c2a8782",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_key = \"biwZehUGhGp1ZwtyL0OaDDtSsod-tqIz\"\n",
    "API_sercret = \"-KEyjyw1V66uzlHIAMuE95MfjePAk71B\"\n",
    "\n",
    "def face_detect(API_key,API_sercret,image_url1,image_url2):\n",
    "    \"\"\"face++ 人脸对比\"\"\"\n",
    "    import requests\n",
    "    url = 'https://api-cn.faceplusplus.com/facepp/v3/compare'\n",
    "    payload = {\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_sercret,\n",
    "        'image_url1':image_url1,\n",
    "        'image_url2':image_url2\n",
    "    }\n",
    "    r = requests.post(url,params=payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "ae5a564b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647346087,3f3c0367-8185-46c3-bf45-d7b966f45b2c',\n",
       " 'time_used': 597,\n",
       " 'confidence': 88,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975},\n",
       " 'faces1': [{'face_token': '0beed7e55f558a087ed95e1a805a3033',\n",
       "   'face_rectangle': {'top': 200, 'left': 125, 'width': 196, 'height': 196}}],\n",
       " 'faces2': [{'face_token': '00d6242028b5c39641417896f9b807de',\n",
       "   'face_rectangle': {'top': 278, 'left': 355, 'width': 427, 'height': 427}}],\n",
       " 'image_id1': 'MxvD11zI3ASGthnrVPDh6Q==',\n",
       " 'image_id2': 'ult5r5MyhxFNNuRqOrWrNg=='}"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_detect(API_key,API_sercret,'https://p3.ssl.qhimgs1.com/sdr/400__/t017b24f4174c4e6ec3.jpg','https://p4.itc.cn/q_70/images03/20210610/cc6939e0db6548d3837beb159b06959e.jpeg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1ccce675",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
